<?php

namespace App\Http\Controllers\Admin\System;

use App\Enums\ResponseEnum;
use App\Http\Controllers\AdminController;
use App\Models\System\Dic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DicController extends AdminController
{
    public function data(Request $request)
    {
        $model = Dic::query()->select(['group_id as dic', 'name', 'key', 'status']);
        if ($groupId = $request->get('groupId')) {
            $model->where('group_id', (int)$groupId);
        }
        $total = $model->count();
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $list = $model->offset(($page - 1) * $pageSize)->limit($pageSize)->get()->toArray();
        return $this->succeed([
            "total" => $total,
            "rows" => $list
        ]);
    }

    public function item(Request $request)
    {
        $model = Dic::query()->select(['system_dic.name as label', 'system_dic.key as value']);
        if ($name = $request->get('name')) {
            $model->whereIn('group_id', function ($query) use ($name) {
                $query->select('id')->from('system_dic_group')->where('code', $name);
            });
        }
        $list = $model->where('system_dic.status', 1)->get()->toArray();
        return $this->succeed($list);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required','max:50'],
            'code' => ['required','max:90','unique:system_dic'],
        ]);
        $data = $request->only(['parent_id', 'name', 'code']);
        DB::beginTransaction();
        try {
            $model = Dic::query()->create([
                'parent_id' => (int)$data['parent_id'],
                'name' => $data['name'],
                'code' => $data['code']
            ]);
            $this->createSyslog('添加了一条字典分类记录', $data, $model);
            DB::commit();
            return $this->succeed([
                'id' => $model['id'],
            ]);
        }catch (\Exception) {
            DB::rollBack();
        }
        return $this->fail();
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => ['required','max:50'],
            'code' => ['required','max:90','unique:system_dic,code,'.$id.',id'],
        ]);
        $model = Dic::query()->findOrFail($id);
        $data = $request->only(['parent_id', 'name', 'code']);
        DB::transaction(function () use ($model, $data) {
            $model->update([
                'parent_id' => (int)$data['parent_id'],
                'name' => $data['name'],
                'code' => $data['code']
            ]);
            $this->createSyslog('更新了一条字典分类记录', $data, $model);
        });
        return $this->succeed();
    }

    public function destroy(Request $request)
    {
        $request->validate(['ids' => ['required','array','max:'.static::QUERY_LIMIT]]);
        $ids = $request->get('ids');
        abort_if(in_array(1, $ids, true), ResponseEnum::ROLE_EXISTS_DELETE_ERROR);
        DB::transaction(function () use ($ids) {
            $rows = Dic::query()->whereIn('id', $ids)->limit(static::QUERY_LIMIT)->delete();
            $this->createSyslog('批量删除了'.$rows.'条字典分类记录', ['ids' => implode(',', $ids)]);
        });
        return $this->succeed();
    }
}
